package com.cskaoyan.shopping.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.shopping.dto.AllProductResponse;
import com.cskaoyan.shopping.dto.CartProductDto;
import com.cskaoyan.shopping.dto.PanelDto;
import com.cskaoyan.shopping.form.PageRequest;
import com.cskaoyan.shopping.service.ICartService;
import com.cskaoyan.shopping.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Set;

/**
 * @author chengyi
 * @date 2022/10/25 20:27
 */
@RestController
@RequestMapping("/shopping")
public class ItemController {

    @Autowired
    ICartService iCartService;

    @Autowired
    IProductService iProductService;


    /**
     * 分页查询商品列表
     * @param
     * @return
     */
    @RequestMapping("/goods")
    public ResponseData getAllGoods(PageRequest pageInfo) {

        AllProductResponse allProduct = iProductService.getAllProduct(pageInfo);
        return new ResponseUtil().setData(allProduct,null);
    }

    /**
     *  查询推荐商品
     * @return
     */
    @RequestMapping("/recommend")
    public ResponseData getRecommendGoods() {
        Set<PanelDto> recommendGoods = iProductService.getRecommendGoods();
        return new ResponseUtil().setData(recommendGoods);
    }



    /**@Description: 获得购物车列表
     * @author chengyi
     * @date 2022/10/26 16:54
     */
    @RequestMapping("/carts")
    public ResponseData getCartsGoods(HttpServletRequest request){
        String userInfo = request.getHeader("user_info");
        JSONObject object = JSON.parseObject(userInfo);
        long userId = Long.parseLong(object.get("uid").toString());

        List<CartProductDto> cartListById = iCartService.getCartListById(userId);
        return new ResponseUtil().setData(cartListById,null);
    }




}
